package com.example.shaidemo;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

public class UploadCarActivity extends AppCompatActivity implements View.OnClickListener {

    EditText type,time,start,end,cost,mileage,duration,city;
    String type_txt,time_txt,start_txt,end_txt,cost_txt,mileage_txt,duration_txt,city_txt;
    ImageView photo;

    private static final String TAG = "UploadCarActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_car);

        // 获取账号

        type = findViewById(R.id.c_type_input);
        time = findViewById(R.id.c_time_input);
        start = findViewById(R.id.c_start_input);
        end = findViewById(R.id.c_end_input);
        cost = findViewById(R.id.c_cost_input);
        mileage = findViewById(R.id.c_mileage_input);
        duration = findViewById(R.id.c_duration_input);
        city = findViewById(R.id.c_city__input);

        //返回按钮
        ImageView pic = findViewById(R.id.collection_pic);
        pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(UploadCarActivity.this,MainActivity.class);
                intent.putExtra("fragment_flag",2);
                startActivity(intent);
            }
        });

        //提交照片
        photo = findViewById(R.id.c_imageView);
        photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //执行OpenCamera（）方法
                OpenCamera();

            }
        });
    }

    @Override
    public void onClick(View view) {
        // 获取用户输入内容
        type_txt = type.getText().toString().trim();
        time_txt = time.getText().toString().trim();
        start_txt = start.getText().toString().trim();
        end_txt = end.getText().toString().trim();
        cost_txt = cost.getText().toString().trim();
        mileage_txt = mileage.getText().toString().trim();
        duration_txt = duration.getText().toString().trim();
        city_txt = city.getText().toString().trim();

        Log.i(TAG, "输入内容: 类型"+type_txt+"   时间："+time_txt+"   起点："+start_txt+"   终点："+end_txt);
        Log.i(TAG, "输入内容: 花费"+cost_txt+"   里程："+mileage_txt+"   时长："+duration_txt+"   城市："+city_txt);

        // 判断输入是否为空、是否合法
        CheckInput ck = new CheckInput();
        boolean flag = ck.CheckInputNull(type_txt) && ck.CheckInputNull(time_txt) && ck.CheckInputNull(start_txt) && ck.CheckInputNull(end_txt);
        boolean flag1 = ck.CheckInputNull(cost_txt) &&ck.CheckInputNull(mileage_txt) && ck.CheckInputNull(duration_txt) && ck.CheckInputNull(city_txt);
        if(flag && flag1){
            Log.i(TAG, "onClick: 全部输入不为空！");
            // 检查数字型数据是否合法
//            boolean f = ck.CheckNumber(time_txt) && ck.CheckNumber(cost_txt) && ck.CheckNumber(mileage_txt);
//            if(f){
                //获取上传时间
                Date date = new Date(System.currentTimeMillis());
                SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
                String time =  sdf.format(date);
                // 将数据存储到数据库
                SharedPreferences sharedPreferences = getSharedPreferences("newCar", Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("class","打车");
                editor.putString("status","未审核");
                editor.putString("time",time);
                editor.putString("start",start_txt);
                editor.putString("end",end_txt);
                String str = "里程："+mileage_txt+"km  ￥ "+cost_txt;
                editor.putString("mes",str);
                editor.commit();
                Log.i(TAG, "onActivityResult: 数据已保存到sharedPreferences");

                //提示消息
                Toast.makeText(this,"上传成功！",Toast.LENGTH_SHORT).show();

                // 返回页面
                finish();
//            }else{
//                //存在非数字
//                Toast.makeText(this,"请输入规范的数据！！",Toast.LENGTH_SHORT).show();
//            }
        }else{
            // 提示信息
            Toast.makeText(this,"输入不能为空，请再次检查输入！",Toast.LENGTH_SHORT).show();
        }

    }

    //打开拍照界面
    public void OpenCamera(){
        Intent open = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(open,2);
    }

    // 获取相机所拍到的图片，并执行SaveScreenShot()方法保存图片
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode == 2 && resultCode == RESULT_OK && null !=data) {
            Bundle bundle = data.getExtras();
            //获取相机返回的数据，并转换为Bitmap图片格式，即缩略图
            Bitmap bitmap = (Bitmap) bundle.get("data");
            //保存图片
            SavePicture sa = new SavePicture();
            String path = sa.SaveScreenShot(bitmap, this);
            Log.i(TAG, "onActivityResult: Uri ===" + path);
            //显示所拍摄的照片
            photo.setImageBitmap(bitmap);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

}